<template>
  <div class="app-container">
    <u-page title="问卷推送（批量）">
      <el-form ref="dialogForm" :model="dataForm" :rules="rules" label-position="left" label-width="120px" style="margin-left: 40px">
        <el-form-item prop="questionnaireId" label="推送问卷">
          <el-select v-model="dataForm.questionnaireId" clearable placeholder="请选择要推送的问卷" style="width: 500px">
            <el-option
              v-for="item in questionnaire"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="remark" label="操作备注信息">
          <el-input v-model="dataForm.remark" style="width:500px;" placeholder="请输入备注以方便追溯历史原因(必填)" clearable maxlength="30" show-word-limit />
        </el-form-item>

        <el-form-item prop="content" label="操作名单">
          <el-input v-model="dataForm.content" type="textarea" placeholder="批量操作名单,手机号" clearable rows="10" />
          <u-tip-info title="格式: 手机号+换行或者英文逗号" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submit">确认操作</el-button>
        </el-form-item>

      </el-form>
    </u-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      api: this.$u.api.PluginsQuestionnaireMassdelivery,
      dialogMemberChoose: false,
      dataForm: {
        questionnaireId: '',
        note: '',
        // 选中的会员卡号
        content: ''
      },
      rules: {
        questionnaireId: [
          { required: true, message: '请选择推送问卷', trigger: 'change' }
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: ['blur'] }
        ],
        content: [
          { required: true, message: '请输入批量操作名单', trigger: ['blur'] }
        ]
      },
      // 问卷集合
      questionnaire: []
    }
  },
  created() {
    this.questionnaireSet()
  },
  methods: {
    // 获取推送问卷列表
    questionnaireSet() {
      const data = {
        push: true // 推送true,不推送false
      }
      this.$u.api.PluginsQuestionnaireNewlist.list(data).then(res => {
        this.questionnaire = res.data.list
      })
    },

    // 确认操作
    submit() {
      const filteredQuestionnaire = this.questionnaire.filter(item => item.id === this.dataForm.questionnaireId)
      const title = filteredQuestionnaire.length > 0 ? filteredQuestionnaire[0].title : ''
      const self = this
      // 效验数据格式
      self.$refs['dialogForm'].validate((valid) => {
        if (valid) {
          // 模态对话框组件
          self.$confirm('此操作将批量推送【' + title + '】问卷，是否继续？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
            // 提交数据
            self.api.submit(self.dataForm).then(() => {
              // 初始化数据
              self.dataForm = {
                questionnaireId: '',
                note: '',
                // 选中的会员卡号
                content: ''
              }
              // 清除表单验证状态
              self.$nextTick(() => {
                self.$refs['dialogForm'].clearValidate()
              })
              // 通知信息
              self.$u.msg('批量推送成功')
            })
          }).catch(() => {
          })
        }
      })
    }
  }
}
</script>
